package com.zhao.service.serviceImpl;

import com.zhao.Others.DateAndTimeObject;
import com.zhao.Others.FileOperationObject;
import com.zhao.Others.GenerateObject;
import com.zhao.dao.DrivingLicenseDao;
import com.zhao.dao.EbickDao;
import com.zhao.dao.StudentDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.EbickService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@Transactional
public class EbickServiceImpl implements EbickService {

    @Autowired
    EbickDao ebickDao;

    @Autowired
    UniversityDao universityDao;

    @Autowired
    DrivingLicenseDao drivingLicenseDao;

    @Autowired
    StudentDao studentDao;

    @Override
    public List<Ebick> findAll(String universityID) {
        return null;
    }

    //////////////////////// admin /////////////////////////////////
    @Override
    public String adminGetAllEbick(Admin admin, HttpSession session) {

        List<Ebick> ebickList = ebickDao.findAll(admin.getUniversityID());
        session.setAttribute("adminEbickList",ebickList);

        return "success";
    }

    @Override
    public String adminGetAllEbickNum(Admin admin, HttpSession session) {

        List<Ebick> ebickList = ebickDao.findAll(admin.getUniversityID());
        int ebickNum = ebickList.size();
        session.setAttribute("ebickNum",ebickNum);

        return "success";
    }

    @Override
    public String adminSeeOneEbick(Admin admin, String ebickID, HttpSession session) {

        Ebick ebick = ebickDao.findByID(ebickID,admin.getUniversityID());
        if(ebick == null){
            return "车牌号错误";
        }
        session.setAttribute("adminOneEbick",ebick);

        return "success";
    }


    /////////////////////// 学生 ///////////////////////////////////

    /**
     * 1.保存图片 2.生成车牌号、保存电动车信息 3.生成行驶证、保存
     * @param ebick
     * @param file
     * @param student
     * @return
     */
    @Override
    public String ebickRegister(Ebick ebick, MultipartFile file ,Student student) {

        Ebick ebick1 = ebickDao.findByID(ebick.getEbickID(),student.getUniversityID());
        if(ebick1 != null){
            //System.out.println("");
            return "error";
        }

        //保存图片
        String visitPath= FileOperationObject.savePicture(file,"ebickpictures",student.getUniversityID(),student.getStudentID());
        if(visitPath == null){//保存失败
            return "error";
        }
        ebick.setImage(visitPath);

        /**
         * 查询学校简称  传给生成函数
         */
        System.out.println(student);
        University university = universityDao.findByID(student.getUniversityID());
        System.out.println(university);
        String shortName = university.getUniversityShortName();

        //生成车牌号
        String ebickID= "";
        //一直到生成一个没有用到的ebickID
        while (true){

            ebickID = GenerateObject.generateEbickID(shortName);

            Ebick temp = ebickDao.findByID(ebickID,student.getUniversityID());

            if(temp == null) break;
            else {
                System.out.println("重复");
            }
        }
        ebick.setEbickID(ebickID);


        //生成电动车 首次注册时间
        String firstRegistTime = DateAndTimeObject.getNowDate();
        ebick.setFirstRegistTime(firstRegistTime);

        //首次注册 的 学生学号
        ebick.setFirstRegistStudentID(student.getStudentID());

        //当前车主ID
        ebick.setNowOwnerID(student.getStudentID());

        //生成行驶证明之前 属于  无证  状态
        ebick.setStatus("无证");

        //设置学校
        ebick.setUniversityID(student.getUniversityID());

        System.out.println(ebick);
        ///////电动车信息  保存到数据库
        int res = ebickDao.add(ebick);
        if(res == 0){
            return "error";
        }

        //学生电动车数量加一
        student.setEbickAmount(student.getEbickAmount()+1);
        res = studentDao.updateStudent(student);

        //生成行驶证号 //生成行驶证
        //DrivingLicense drivingLicense = GenerateObject.generateDrivingLicense(student,ebick);
        //drivingLicense.setStatus("正常");

        ///生成行驶证明后  为  有证  状态
        //ebick.setStatus("有证");


        //行驶证信息  保存到数据库
        //int res1 = drivingLicenseDao.add(drivingLicense);
        //System.out.println(drivingLicense);
        //if(res1 != 1){
        //    return "error";
        //}

        return "success";
    }

    @Override
    public String findAllMyEbick(HttpSession session) {

        System.out.println("查看电动车");
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            System.out.println("请先登录");
            return "error";
        }

        List<Ebick> haveEvidenceebickList = ebickDao.findAllByNowOwnerIDAndStatus(student.getStudentID(),"有证",student.getUniversityID());
        List<Ebick> notHaveEvidenceebickList = ebickDao.findAllByNowOwnerIDAndNotStatus(student.getStudentID(),"有证",student.getUniversityID());
        for(Ebick ebick : notHaveEvidenceebickList){
            if(ebick.getStatus().equals("已驳回")){
                ebick.setStatus(ebick.getStatus()+": "+ebick.getReason());
            }
        }
        System.out.println(haveEvidenceebickList);
        System.out.println(notHaveEvidenceebickList);

        session.setAttribute("haveEvidenceebickList",haveEvidenceebickList);
        session.setAttribute("notHaveEvidenceebickList",notHaveEvidenceebickList);

        if(haveEvidenceebickList.size()==0){
            session.removeAttribute("haveEvidenceebickList");
        }
        if(notHaveEvidenceebickList.size()==0){
            session.removeAttribute("notHaveEvidenceebickList");
        }


        System.out.println("查看完毕");

        return "success";
    }

    @Override
    public String getMyStudentEbickNum(SchoolCounsellor schoolCounsellor, HttpSession session) {

        List<Ebick> ebickList = ebickDao.findAll(schoolCounsellor.getUniversityID());
        int myStudentEbickNum = 0;
        for(Ebick ebick : ebickList){
            Student student = studentDao.findByStudentID(ebick.getNowOwnerID(),schoolCounsellor.getUniversityID());
            if(student.getMajorID().equals(schoolCounsellor.getMajorID()))myStudentEbickNum++;
        }
        session.setAttribute("myStudentEbickNum",myStudentEbickNum);

        return "success";
    }


}
